/*===========================================================================
  Copyright (C) 2014 by the Okapi Framework contributors
-----------------------------------------------------------------------------
  This library is free software; you can redistribute it and/or modify it 
  under the terms of the GNU Lesser General Public License as published by 
  the Free Software Foundation; either version 2.1 of the License, or (at 
  your option) any later version.

  This library is distributed in the hope that it will be useful, but 
  WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this library; if not, write to the Free Software Foundation, 
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

  See also the full LGPL text here: http://www.gnu.org/copyleft/lesser.html
===========================================================================*/

package net.sf.okapi.steps.quest;

import net.sf.okapi.common.IParameters;
import net.sf.okapi.common.LocaleId;
import net.sf.okapi.common.annotation.GenericAnnotation;
import net.sf.okapi.common.annotation.GenericAnnotationType;
import net.sf.okapi.common.annotation.GenericAnnotations;
import net.sf.okapi.common.resource.ISegments;
import net.sf.okapi.common.resource.ITextUnit;
import net.sf.okapi.common.resource.Segment;
import net.sf.okapi.common.resource.TextFragment;

public class QuestProcessor {

	private Parameters params = new Parameters();
	private LocaleId targetLocale;

	public Parameters getParameters () {
		return params;
	}
	
	public void setParameters (IParameters params) {
		this.params = (Parameters)params;
	}
	
	public void setTargetLocale (LocaleId targetlocale) {
		this.targetLocale = targetlocale;
	}

	public void processTextUnit (ITextUnit tu) {
		// Skip non-translatable
		if ( !tu.isTranslatable() ) return;
		// Skip if no target
		if ( !tu.hasTarget(targetLocale) ) return;
		
		// One possible content to process: the target itself
		// Go through the segments
		ISegments srcSegs = tu.getSourceSegments();
		ISegments trgSegs = tu.getTarget(targetLocale).getSegments();
		for ( Segment srcSeg : srcSegs ) {
			Segment trgSeg = trgSegs.get(srcSeg.getId());
			if ( trgSeg == null ) continue; // Skip if target is not present
			// Process the segment
			processSegment(srcSeg, trgSeg);
		}
	}
	
	private void processSegment (Segment srcSeg,
		Segment trgSeg)
	{
		// Get the target content
		TextFragment trgFrag = trgSeg.getContent();
		// TODO: Do the QuEst magic!
		double questScore = 89.12;
		// One possible way to attach the result: inline annotation
		// Create the annotation
		GenericAnnotations anns = new GenericAnnotations(
			new GenericAnnotation(GenericAnnotationType.LQR,
				GenericAnnotationType.LQR_SCORE, questScore));
		trgFrag.annotate(0, trgFrag.getCodedText().length(), GenericAnnotationType.GENERIC, anns);
	}

}
